<template>
	<view class="entry-content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="title">请输入您的神秘代码</view>
		<input type="text" class="input" placeholder="请输入您的神秘代码" v-model="workNumber"/>	
		<view class="error">
			<span v-if="errorInfo">{{errorInfo}}</span>
		</view>
		<view class="btns">
			<button type="default" class="cancelbtn"  @click="cancel">取消</button>
			<button type="default" class="okBtn" open-type="getUserInfo" @getuserinfo="getUserInfo">确定</button>
		</view>
	</view>
</template>

<script>
	import { userLogin } from '@/api/index.js'
	import {config} from "@/utils/utils.js"
	
	
	export default {
		name:"entry",
		data() {
			return {
				workNumber: '',
				errorInfo:'',
			};
		},
		methods: {
			/**
			 * 规则验证
			 * 工号规则：八位数，首位数字1、2、4、5
			*/
			validRules() {
				var valid = true;
				if(!this.workNumber) {
					this.errorInfo = '请输入工号';
					valid = false;
				}else if(this.workNumber.length != 8) {
					this.errorInfo = '抱歉，输入工号不正确';
					valid = false;
				}else {
					let numberFirst = this.workNumber.substr(0,1);
					if (numberFirst != 1 && numberFirst != 2 && numberFirst != 4 && numberFirst == 5) {
						this.errorInfo = '抱歉，输入工号不正确';
						valid = false;
					}
				}
				if (valid == true) this.errorInfo = '';
				return valid;
			},
			cancel() {
				uni.exitMiniProgram({
					success: function() {
						console.log('退出小程序成功');
					},
					fail: function(err) {
						console.log('退出小程序失败', err);
					}
				})
			},
		//获取用户信息
			getUserInfo(e) {
				if(!this.validRules()) return;
				console.log("登录==",this.workNumber)
				uni.showLoading({
					mask:true,
					title: "登录中..."
				})
				let that = this;
				var p = this.getSetting();
				p.then(function(isAuth) {
				
					console.log('是否已经授权', isAuth);
					if (isAuth) {
						//eData  包括//微信头像//微信名称 还有加密的数据
						// 默认信息 不可以
						let eData = JSON.parse(e.detail.rawData);
						console.log("eData===",eData)
						console.log('e.target===',e.target)
			
						uni.login({
							success(res) {
								let param = {
									sign: that.workNumber,
									code: res.code,
									rawData: e.detail.rawData,
									encryptedData: e.target.encryptedData,
									iv: e.target.iv,
									signature: e.target.signature
								}
								
								/**调用登录接口*/
								userLogin(param).then(res=> {
									console.log("用户信息res====",res)
									// uni.showToast({
									// 	title:"获取用户信息成功",
									// 	icon:"success"
									// });
									// uni.setStorageSync(config.sessionId,res.content.session);
									uni.setStorageSync(config.userInfo,res.info);
									that.$emit('close',true);
								})
								console.log("param====",param)
							},
							fail(error) {
								uni.showToast({
									title:"获取用户信息失败",
									icon:"none"
								})
							}
						})
					} else {
						uni.showToast({
							title: '授权失败，请确认授权已开启',
							mask: true,
							icon: 'none'
						})
					}
				});
			},
			//获取用户的当前设置
			getSetting() {
				return new Promise(function(resolve, reject) {
					uni.getSetting({
						success: function(res) {
							if (res.authSetting['scope.userInfo']) {
								console.log('存在');
								console.log("res.authSetting===",res.authSetting)
								resolve(true);
							} else {
								console.log('不存在');
								resolve(false);
							}
						}
					})
				}).catch((e) => {
					console.log(e)
				});
			}
			
		}
	}
</script>

<style lang="scss" scoped>
	.entry-content {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		height: 100%;
		background-color: rgba(255, 255, 255, 0.9);
		position: fixed;
		pointer-events:auto;
		top: 0;
		left: 0;
		 -webkit-user-drag: none;
		  user-select: none;
		  -moz-user-select: none;
		  -webkit-user-select: none;
		  -ms-user-select: none;
		.logo {
			width: 141rpx;
			height: 90rpx;
			margin: 90rpx auto 60rpx auto;
		}
		.title {
			margin-top: 180rpx;
		}
		.input {
			width: 650rpx;
			height: 60rpx;
			color: #000000;
			text-align: center;
			padding:0 24rpx;
			border-radius: 10rpx;
			height: 60rpx;
			background: transparent;
			border: 1px solid #000000;
			font-size: 28rpx;
			margin-top: 40rpx;
		}
		.error {
			margin: 50rpx auto;
			height: 100rpx;
			width: 100%;
			text-align: center;
			color: red;
		}
		.btns {
			height: 80rpx;
			width: 100%;
			display: flex;
			align-items: center;
			.cancelbtn {
				border-radius: 10rpx;
				background: white;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 240rpx;
				height: 100%;
				font-size: 30rpx;
				color: rgb(6, 193, 96);		
				margin-right: 0;
			}
			.okBtn {
				border-radius: 10rpx;
				background: rgb(6, 193, 96);
				display: flex;
				justify-content: center;
				align-items: center;
				width: 240rpx;
				height: 100%;
				font-size: 30rpx;
				color: white;
				margin-left: 50rpx;
			}
		}
	}


</style>